package cn.four.hqServer.modules.deposit.service.impl;

import cn.four.hqServer.common.entity.HqCardInfo;
import cn.four.hqServer.common.entity.HqTranUser;
import cn.four.hqServer.common.entity.HqYhTran;
import cn.four.hqServer.common.enums.CardStatusEnum;
import cn.four.hqServer.common.result.Result;
import cn.four.hqServer.modules.deposit.dto.DeposDto;
import cn.four.hqServer.modules.deposit.mapper.CxmHqCardInfoMapper;
import cn.four.hqServer.modules.deposit.mapper.CxmHqTranUserMapper;
import cn.four.hqServer.modules.deposit.mapper.CxmHqYhTranMapper;
import cn.four.hqServer.modules.deposit.service.CxmUserDepositService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;


/**
 * 增加用户存款账户余额
 *
 * @author cccxm
 * @version 1.0
 */
@Slf4j
@Service
@Transactional  //运行时异常
public class CxmUserDepositServiceImpl extends ServiceImpl<CxmHqCardInfoMapper, HqCardInfo> implements CxmUserDepositService {

    @Resource
    private CxmHqTranUserMapper cxmHqTranUserMapper;

    @Resource
    private CxmHqCardInfoMapper cxmHqCardInfoMapper;

    @Resource
    private CxmHqYhTranMapper cxmHqYhTranMapper;

    /**
     * 根据卡号获取卡信息
     *
     * @param cardId 银行卡号
     * @return 银行卡信息
     */
    @Override
    public HqCardInfo getCardInfo(String cardId) {
        QueryWrapper<HqCardInfo> cardInfoQuery = new QueryWrapper<>();
        return this.getOne(cardInfoQuery.eq("card_id", cardId));
    }

    /**
     * 更新银行卡余额
     *
     * @param hqCardInfo 银行卡信息对象
     */
    @Override
    public int updateBalance(HqCardInfo hqCardInfo) {
        //UPDATE hq_card_info SET balance=? WHERE carId=?
        UpdateWrapper<HqCardInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("card_id", hqCardInfo.getCardId());
        updateWrapper.set("balance", hqCardInfo.getBalance());
        return cxmHqCardInfoMapper.update(hqCardInfo, updateWrapper);
    }

    /**
     * 存款
     *
     * @param deposDto
     * @return
     * @throws InterruptedException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
    public Result saveMoney(DeposDto deposDto) throws InterruptedException {

        //获取输入的金额, 判断是否有误
        if (deposDto.getTranAmount().intValue() <= 0) {
            return Result.error(500, "输入的金额有误,请重新输入!");
        }

        //获取卡信息
        HqCardInfo hqCardInfo = this.getCardInfo(deposDto.getCardId());
        System.out.println(hqCardInfo);
        if (hqCardInfo == null) {
            return Result.error(404, "该账户不存在");
        }

        //1.通过银行卡验证用户账户状态
        //if (hqCardInfo.getState() != 0) { //0正常
        if (!hqCardInfo.getState().equals(CardStatusEnum.normal.getCode())) { //0正常
            //当前账户状态不正常
            System.out.println(Result.error("账户状态异常，请用户出示身份证更新账户状态!!!"));
            //模拟操作过程
            Thread.sleep(500);
            //更新账户状态
            hqCardInfo.setState(0);
        }

        //2.增加用户存款账户余额  加法：add  减法：subtract()
        //HqCardInfo balance = userDepositService.getOne(new QueryWrapper<HqCardInfo>().eq("card_id", cardId));
        BigDecimal balance = hqCardInfo.getBalance().add(deposDto.getTranAmount());
        //2.1更新用户银行卡修改时间
        hqCardInfo.setUpdateTime(LocalDateTime.now());
        //2.2更新用户银行卡余额
        hqCardInfo.setBalance(balance);
        int i = this.updateBalance(hqCardInfo);

        if (i > 0) {
            //3.记录用户交易流水 - 用户流水表插入一条数据
            //cxmUserFlowService.userFlowDepositInsert(cardId, BigDecimal.valueOf(money), balance);
            HqTranUser hqTranUser = new HqTranUser();
            hqTranUser.setCardId(deposDto.getCardId());
            hqTranUser.setBusinessType(2);//存款2
            hqTranUser.setTranAmount(deposDto.getTranAmount());
            hqTranUser.setTranAfterAmount(balance);
            hqTranUser.setTranTime(LocalDateTime.now());
            hqTranUser.setTranName("招商银行现金账户");
            hqTranUser.setTranCardId("1234567890123456");
            hqTranUser.setNotes("柜面存款");
            cxmHqTranUserMapper.insert(hqTranUser);

            //4.记录现金交易流水 - 现金流水表插入一条数据
            HqYhTran hqYhTran = new HqYhTran();
            hqYhTran.setCardId(deposDto.getCardId());//银行卡号
            hqYhTran.setBusinessType(2);//业务类型
            hqYhTran.setTranAmount(deposDto.getTranAmount());//交易金额
            hqYhTran.setTranTime(LocalDateTime.now());//交易时间
            cxmHqYhTranMapper.insert(hqYhTran);

            //5.增加现金总账账户余额
            //6.记录总账交易流水
        }

        //6.用户存款成功
        return Result.success("用户存款成功!");
    }
}
